package org.op.editor;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.swing.JEditorPane;
import javax.swing.JOptionPane;

import org.op.FrameView;
import org.op.editor.doc.EditorLoadController;
import org.op.menu.OpFileChooser;
import org.op.service.WwDocumentInfo;
import org.op.service.config.ConfigKeys;
import org.op.service.config.ConfigurationService;
import org.op.service.fileio.FileRWService;
import org.op.service.log.LoggerService;
import org.op.service.wwcache.WikiwordCacheService;
import org.op.util.MessageController;

public class OpenCloseControllerImpl implements OpenCloseController {

	private EditorComponentView editorView;
	private EditorLoadController editorController;
	private FrameView frameView;
	private FileRWService fileRWService;
	private ConfigurationService configService;
	private WikiwordCacheService wwService;
	private MessageController messageController;
	private LoggerService logger;
	
	public void closeDocument() {
		// check to see if dirty
		try {
			if (editorView.getCurrentPane()!=null) {
				if (editorView.isCurrentDirty()) {
					String message = messageController.getMessage("confirm.dirty.close");
					int confirm = JOptionPane.showConfirmDialog(frameView.getFrame(), message);
					if (confirm==JOptionPane.YES_OPTION) {
						editorView.removeSelectedEditor();
					}
				} else {
					editorView.removeSelectedEditor();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void closeAllDocuments() {
		// Better idea -- check to see if any dirty..
		//  If any docs are dirty, then ask to save all or cancel
		//  Continue only if save all is complete
		
		// keep closing, until you hit a stopping point (someone says not to close)
		JEditorPane nextPane = editorView.getCurrentPane();
		while (nextPane!=null) {
			String lastEditorName = nextPane.getName();
			closeDocument();
			nextPane = editorView.getCurrentPane();
			if (nextPane!=null && nextPane.getName().equalsIgnoreCase(lastEditorName)) {
				nextPane = null;
			}
		}
	}
	
	public void exitApplication() {
		closeAllDocuments();
		JEditorPane nextPane = editorView.getCurrentPane();
		if (nextPane==null) {
			System.exit(0);
		}
	}

	
	public void newDocument() {
		// Get a new name (i.e; Untitled)
		String name = "untitled";
		
		// find if there are any other untitled docInfos -- can't have two of the same docInfos
		Map<WwDocumentInfo, JEditorPane> editorData = editorView.getEditorPanes();
		int count = 1;
		boolean found = true;  // assume exists in the editor pane
		while (found) {
			found = false;
			for (WwDocumentInfo exInfo : editorData.keySet()) {
				if (exInfo.getName().equalsIgnoreCase(name)) {
					found = true;
				}
			}
			if (found) {
				name = "untitled-" + (new Integer(count)).toString();
				count ++;
			}
		}
		
		WwDocumentInfo docInfo = new WwDocumentInfo(name, null);
		List<String> contents = new ArrayList<String>();
		editorController.loadFileIntoEditor(contents, docInfo);// Opens a file di
	}
	
	public void openDocument() {
		
		String workspacePath = configService.getProperty(ConfigKeys.KEY_CURRENT_WORKSPACE);
		
		// open the filechooser in this directory
		OpFileChooser fileChooser = new OpFileChooser(frameView.getFrame(), workspacePath);
		fileChooser.setFilter(new TextFileFilter()); // Dependency?
		File newFile = fileChooser.openAndGet();
		String fileName = newFile.getName();
		
		// dont include the file extension??
		int i = fileName.indexOf('.');
		fileName = fileName.substring(0,i);
		
		try {
			List<String> file = fileRWService.readFileText(newFile.getAbsolutePath());
			WwDocumentInfo docInfo = new WwDocumentInfo(fileName, newFile.getAbsolutePath());
			editorController.loadFileIntoEditor(file, docInfo);
		} catch (Exception e) {
			e.printStackTrace();
			frameView.showExceptionDialog(e.getLocalizedMessage());
		}

	}

	public void openWikiwordDocument(String wikiword) {
		try {
			WwDocumentInfo docInfo = wwService.getInfoForWikiword(wikiword);
			if (docInfo!=null && docInfo.getFilepath()!=null) {
				List<String> file = fileRWService.readFileText(docInfo.getFilepath());
				editorController.loadFileIntoEditor(file, docInfo);
			}
			
		} catch (Exception e) {
			frameView.showExceptionDialog(e.getLocalizedMessage());
		}
		
	}
	
	public EditorComponentView getEditorView() {
		return editorView;
	}


	public void setEditorView(EditorComponentView editorView) {
		this.editorView = editorView;
	}


	public EditorLoadController getEditorController() {
		return editorController;
	}


	public void setEditorController(EditorLoadController editorController) {
		this.editorController = editorController;
	}


	public FrameView getFrameView() {
		return frameView;
	}


	public void setFrameView(FrameView frameView) {
		this.frameView = frameView;
	}


	public FileRWService getFileRWService() {
		return fileRWService;
	}


	public void setFileRWService(FileRWService fileRWService) {
		this.fileRWService = fileRWService;
	}


	public ConfigurationService getConfigService() {
		return configService;
	}


	public void setConfigService(ConfigurationService configService) {
		this.configService = configService;
	}

	public WikiwordCacheService getWwService() {
		return wwService;
	}

	public void setWwService(WikiwordCacheService wwService) {
		this.wwService = wwService;
	}

	public MessageController getMessageController() {
		return messageController;
	}

	public void setMessageController(MessageController messageController) {
		this.messageController = messageController;
	}

	public LoggerService getLogger() {
		return logger;
	}

	public void setLogger(LoggerService logger) {
		this.logger = logger;
	}





}
